package liming.ai.code.java_maven.info;

import liming.ai.code.java_maven.tool.ToJsonInterface;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Objects;

public class JavaInfo extends BaseConfig implements ToJsonInterface {
    @JsonField("package")
    private final String packageName;
    @JsonField("class")
    private final String className;

    public JavaInfo(ModeInfo modeInfo, String packageName, String className) {
        super(modeInfo);
        this.packageName = packageName;
        this.className = className;
    }

    public File getJavaFile() {
        return new File(getJavasPath(), packageName.replace(".", File.separator) + File.separator + className + ".java");
    }

    public File getMdFile() {
        return new File(getMdsFilePath(), packageName.replace(".", File.separator) + File.separator + className + ".md");
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof JavaInfo)) return false;
        JavaInfo javaInfo = (JavaInfo) o;
        return Objects.equals(packageName, javaInfo.packageName) && Objects.equals(className, javaInfo.className);
    }

    @Override
    public int hashCode() {
        return Objects.hash(packageName, className, modeInfo.getModeName());
    }

    @Override
    public String toString() {
        return String.format("mode:%s packet:%s className:%s java文件存在性：%b md文件存在性: %b", modeInfo.getModeName(), packageName, className, getJavaFile().isFile(), getMdFile().isFile());
    }

    @Override
    public JSONObject toJsonBody(Class<?> clazz) {
        return ToJsonInterface.super.toJsonBody(clazz).put("mode", modeInfo.getModeName());
    }

    public static List<JavaInfo> loadJavaFiles(ModeInfo modeInfo) throws IOException {
        File file = new File(modeInfo.getModePath(), JAVA_SOURCE_DIR);
        List<JavaInfo> javaInfos = new java.util.ArrayList<>();
        if (file.isDirectory())
            Files.walk(file.toPath())
                    .filter(Files::isRegularFile)
                    .filter(p -> p.toString().endsWith(".java"))
                    .forEach(p -> {
                        try {
                            String relativePath = file.toPath().relativize(p).toString();
                            String qualifiedName = relativePath
                                    .replace(File.separatorChar, '.')
                                    .substring(0, relativePath.length() - ".java".length());

                            int lastDotIndex = qualifiedName.lastIndexOf('.');
                            String packageName = lastDotIndex > 0 ?
                                    qualifiedName.substring(0, lastDotIndex) : "";
                            String className = qualifiedName.substring(lastDotIndex + 1);
                            javaInfos.add(new JavaInfo(modeInfo, packageName, className));
                        } catch (Exception e) {
                            System.err.println("Error processing file: " + p + ", " + e.getMessage());
                        }
                    });
        return javaInfos;
    }
}
